package client;

import java.awt.Point;

import nanoxml.XMLElement;
import utils.ITransmittable;

import commands.Commands;

import enums.PlayerColor;

/**
 * Represents a move that a piece performs from one cell to another.
 */
public class Move implements ITransmittable {
	
	private static final String ATTR_NAME_PLAYER_COLOR_NAME = "PlayerColorName";
	private static final String ATTR_NAME_X1 = "x1";
	private static final String ATTR_NAME_Y1 = "y1";
	private static final String ATTR_NAME_X2 = "x2";
	private static final String ATTR_NAME_Y2 = "y2";
	
	private String playerColorName;
	private int x1, y1, x2, y2;

	
	private Move (String playerColorName, int x1, int y1, int x2, int y2) {
		this.playerColorName = playerColorName;
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
	}

	public Move (XMLElement message) {
		this(message.getAttribute(ATTR_NAME_PLAYER_COLOR_NAME).toString(),
			 message.getIntAttribute(ATTR_NAME_X1), message.getIntAttribute(ATTR_NAME_Y1),
			 message.getIntAttribute(ATTR_NAME_X2), message.getIntAttribute(ATTR_NAME_Y2));
	}
	
	public Move (PlayerColor playerColor, Point from , Point to) { 
		this(playerColor.toString(), (int)from.getX(), (int)from.getY(), (int)to.getX(), (int)to.getY());
	}

	public Move(Move moveToClone)
	{
		this(moveToClone.getPlayerColorName(), moveToClone.getX1(), moveToClone.getY1(), moveToClone.getX2(), moveToClone.getY2());
	}

	
	public String toString()
	{
		return "(" + getX1() + ", " + getY1() + ") to (" + getX2() + ", " + getY2() + ")";
		
	}
	
	/**
	 * @return The color name of the player that made the move.
	 */
	public String getPlayerColorName()
	{
		return playerColorName;
	}
	
	/**
	 * @return Returns the starting x coordinate.
	 */
	public int getX1() {return x1; }

	/**
	 * @return Returns the starting y coordinate.
	 */
	public int getY1() {return y1; }

	/**
	 * @return Returns the ending x coordinate.
	 */
	public int getX2() {return x2; }

	/**
	 * @return Returns the ending y coordinate.
	 */
	public int getY2() {return y2; }
	
	public Point getFrom() {
		return new Point(x1,y1);
	}
	
	public Point getTo() {
		return new Point(x2,y2);
	}

	public XMLElement toXML() {
		XMLElement message = new XMLElement ();
		message.setName(Commands.MOVE);
		
		message.setAttribute(ATTR_NAME_PLAYER_COLOR_NAME, playerColorName);
		message.setIntAttribute (ATTR_NAME_X1, getX1());
		message.setIntAttribute (ATTR_NAME_Y1, getY1());
		message.setIntAttribute (ATTR_NAME_X2, getX2());
		message.setIntAttribute (ATTR_NAME_Y2, getY2());
		
		return message;
	}
}